// read fileList. and open all files in the fileList
// Pause, resume to solve the error for 'open too many files'
var fs=require('fs')
var readLine=require('readline')

var fRead=fs.createReadStream('result\\flist.txt',{encoding:'utf8',start:3})
var fWrite=fs.createWriteStream('out.txt')
var rLine=readLine.createInterface({input:fRead,output:fWrite,terminal: true})
var count=0;
var openCount=0
rLine.on('line', (fName) => {
  count++;
  console.log(count+fName)
  if (count%100==0) {
    console.log('pause in'+count)
    rLine.pause()
  }
  procOneFile(fName)
});
rLine.on('close',() =>{
  console.log('fileList is closed')
  fWrite.end();
})

function procOneFile(oneFile){
  var fIn=fs.createReadStream(oneFile,{encoding:'utf8'})
  var fReader=readLine.createInterface({input:fIn})
  var sentCount=0
  openCount++

  fReader.on('line',(sent)=>{
     sentCount++
     if (sentCount==1) {
      console.log('begin readline'+oneFile)//code
     }
  })
  fReader.on('close',()=>{
    console.log('close '+oneFile)
    openCount--;
    if (openCount==0) {
      rLine.resume()//code
    }
  })
}